package com.ebupt.migu.music.schedule.service.impl;

import com.ebupt.migu.common.pojo.Topic;
import com.ebupt.migu.common.sms.MegamarketRequestBean;
import com.ebupt.migu.music.schedule.service.OfflineMessageProducerService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author liuyangyang@e-byte.com
 * @description: TODO
 * @date 2024/8/15 15:40
 */
@Slf4j
@Service
public class OfflineMessageProducerServiceImpl implements OfflineMessageProducerService {
    private final KafkaTemplate<String, MegamarketRequestBean> kafkaTemplate;

    @Autowired
    public OfflineMessageProducerServiceImpl(KafkaTemplate<String, MegamarketRequestBean> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }


    @Override
    public void offlineMessage(MegamarketRequestBean bean) {
        String requestId = UUID.randomUUID().toString();
        bean.setRequestId(requestId);
        // 添加日志追踪requestId
        MDC.put(Topic.REQUEST_ID_KEY,requestId);
        log.info("离线短信生产者body -> {}",bean);
        if (bean.getExecuteType() == 1) {
            if (bean.getPort() == 1 || bean.getPort() == 2) {
                kafkaTemplate.send(Topic.OFFLINE_SMS_1130, bean);
            } else if (bean.getPort() == 3 || bean.getPort() == 4) {
                kafkaTemplate.send(Topic.OFFLINE_SMS_1131, bean);
            }
        } else {
            kafkaTemplate.send(Topic.OFFLINE_SMS_TEST, bean);
        }
        MDC.remove(Topic.REQUEST_ID_KEY);
    }
}
